package com.wectrls.config;

import lombok.Data;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;

import java.io.Serializable;

/**
 * @ClassName SnowflakeWorkerId
 * @Description //TODO 自定义主键ID生成器
 * @Author 陈建
 * @Date 2019/6/27 15:26
 * @Version 2.0
 */
@Data
public class SnowflakeWorkerId implements IdentifierGenerator {

    public static final String SNOWFLAKE = "SNOWFLAKE";
    public static final String SNOWFLAKE_STRATEGY = "com.wectrls.config.SnowflakeWorkerId";

    @Override
    public Serializable generate(SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws HibernateException {
        //基于雪花算法实现
        return String.valueOf(SnowFlake.nextId());
    }
}
